﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
using CommonTools;
using BaseDll;
using MotionIoLib;
namespace OtherDevice
{
   public class ClylinerBase
    {
        public Form_ControlBase controlFrom = null;
        public ClylinderParamter paramter = new ClylinderParamter();
        public ClylinerBase()
        {
            controlFrom = new Form_LylinerControl();
        }
        public int Init(ClylinderParamter paramter)
        {
            this.paramter = paramter;
            
            return 0;
        }
        public void Function_前进(bool b_WaitInPos=true)
        {
            List<string> listStationName = StationMgr.GetInstance().GetAllStationName();
            if (listStationName.Count == 0)
                throw new Exception("查找不到任何工站信息");
            bool bmanual = GlobalVariable.g_StationState == StationState.StationStateRun ? false : true;
        retry_open:
            Stationbase station = StationMgr.GetInstance().GetStation(listStationName[0]);
            if (paramter.b_是否为双控电磁阀)
            {
                if (paramter.b_是否为机器人气缸)
                {
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_前段输出信号, true);
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_后段输出信号, false);
                }
                else
                {
                    paramter.str_前段输出信号.SetIO(true);
                    Thread.Sleep(paramter.n_双控输出信号时间间隔);
                    paramter.str_后段输出信号.SetIO(false);
                }
            }
            else
            {
                if (paramter.b_是否为机器人气缸)
                {
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_前段输出信号, true);
                }
                else
                {
                    paramter.str_前段输出信号.SetIO(true);
                }
            }
            if (b_WaitInPos)
            {
                if (paramter.str_前段感应到位信号 == "")
                {
                    Thread.Sleep(paramter.n_检测到位延时);
                    return;
                }
                if (paramter.b_是否为机器人气缸)
                {
                    if (RobotMotionMgr.GetInstace().GetRobot(paramter.str_机器人名称).CheckIoByName(paramter.str_前段感应到位信号, true, paramter.n_检测到位延时))
                        return;
                    else
                        throw new Exception($"{paramter.str_前段感应到位信号} 感应失败");
                }
                else
                {
                    WaranResult waranResult = station.CheckIobyName(200, paramter.str_前段感应到位信号, true, bmanual, paramter.n_检测到位延时);
                    if (waranResult == WaranResult.Run)
                        return;
                    else if (waranResult == WaranResult.Custom1)
                        goto retry_open;
                    else
                        throw new Exception($"{paramter.str_前段感应到位信号} 感应失败");
                }

            }
        }
        public void Function_后退(bool b_WaitInPos = true)
        {
            List<string> listStationName = StationMgr.GetInstance().GetAllStationName();
            if (listStationName.Count == 0)
                throw new Exception("查找不到任何工站信息");
            bool bmanual = GlobalVariable.g_StationState == StationState.StationStateRun ? false : true;    
        retry_open:
            Stationbase station = StationMgr.GetInstance().GetStation(listStationName[0]);
            if (paramter.b_是否为双控电磁阀)
            {
                if (paramter.b_是否为机器人气缸)
                {
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_前段输出信号, false);
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_后段输出信号, true);
                }
                else
                {
                    paramter.str_后段输出信号.SetIO(true);
                    Thread.Sleep(paramter.n_双控输出信号时间间隔);
                    paramter.str_前段输出信号.SetIO(false);

                }
            }
            else
            {
                if (paramter.b_是否为机器人气缸)
                {
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_前段输出信号, false);
                }
                else
                {
                    paramter.str_前段输出信号.SetIO(false);
                }
            }
            if (b_WaitInPos)
            {
                if (paramter.str_后段感应到位信号 == "")
                {
                    Thread.Sleep(paramter.n_检测到位延时);
                    return;
                }
                if (paramter.b_是否为机器人气缸)
                {
                    if (RobotMotionMgr.GetInstace().GetRobot(paramter.str_机器人名称).CheckIoByName(paramter.str_后段感应到位信号, true, paramter.n_检测到位延时))
                        return;
                    else
                        throw new Exception($"{paramter.str_后段感应到位信号} 感应失败");
                }
                else
                {
                    WaranResult waranResult = station.CheckIobyName(200, paramter.str_后段感应到位信号, true, bmanual, paramter.n_检测到位延时);
                    if (waranResult == WaranResult.Run)
                        return;
                    else if (waranResult == WaranResult.Custom1)
                        goto retry_open;
                    else
                        throw new Exception($"{paramter.str_后段感应到位信号} 感应失败");
                }
            }
        }
        public void Function_中断停止()
        {
            if(paramter.b_是否为双控电磁阀)
            {
                if (paramter.b_是否为机器人气缸)
                {
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_前段输出信号, false);
                    RobotMotionMgr.GetInstace().SetOutput(paramter.str_机器人名称, paramter.str_后段输出信号, false);
                }
                else
                {
                    paramter.str_前段输出信号.SetIO(false);
                    paramter.str_后段输出信号.SetIO(false);
                }

            }
        }
        public bool Function_CheckState(bool b_前进)
        {
            try
            {
                bool b_State = false;
                string str_CheckInput = b_前进 ? paramter.str_前段感应到位信号 : paramter.str_后段感应到位信号;
                if (paramter.b_是否为机器人气缸)
                {
                    b_State= RobotMotionMgr.GetInstace().GetInBit(paramter.str_机器人名称, str_CheckInput);
                }
                else
                {
                    b_State = str_CheckInput.GetInput();
                }
                return b_State;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}